import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * http请求转发
 */
public class S16_RequestForwardingServlet02 extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    /*
        请求转发注意事项：
        1.浏览器地址不会变化(地址会保留在第一个servlet的url)
        2.在同一次HTTP请求中，进行多次转发，仍然是一次HTTP请求
        3.在同一次HTTP请求中，进行多次转发，多个servlet可以共享request对象/域的数据(因为始终是同一个request对象)
        4.可以转发到 WEB-INF目录下
        5.不能访问当前WEB工程外的资源
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //取出用户名
        String username = req.getParameter("username");
        String role = (String) req.getAttribute("role");

        //输出信息
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.print("用户名：" + username + "<br/>");
        out.print("角色：" + role);
        out.flush();
        out.close();
    }
}
